package org.cable.account.controller;

import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.cable.account.controller.request.SendCodeRequest;
import org.cable.account.service.NotifyService;
import org.cable.common.constants.RedisKey;
import org.cable.common.enums.BizCodeEnum;
import org.cable.common.enums.SendCodeEnum;
import org.cable.common.util.CommonUtil;
import org.cable.common.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author hsj
 * @date 2023/10/26 10:09
 */
@RestController
@RequestMapping("/api/notify/v1")
@Slf4j
public class NotifyController {

    private static final long CAPTCHA_CODE_EXPIRED = 60 * 1000 * 10;

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private Producer producer;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @GetMapping("send_code")
    public JsonData sendCode() {
        notifyService.testSend();
        return JsonData.buildSuccess();
    }


    @GetMapping("/captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) {
        String text = producer.createText();
        log.info("验证码内容:{}", text);

        //配置redis
        redisTemplate.opsForValue().set(getCaptchaKey(request), text, CAPTCHA_CODE_EXPIRED, TimeUnit.MILLISECONDS);
        BufferedImage bufferedImage = producer.createImage(text);
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageIO.write(bufferedImage, "jpg", outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("获取流量出错", e.getMessage());
        }
    }

    private String getCaptchaKey(HttpServletRequest request) {

        String ipAddr = CommonUtil.getIpAddr(request);
        String userAgent = request.getHeader("User-Agent");
        String key = "account-service:captcha:" + CommonUtil.MD5(ipAddr + userAgent);
        log.info("验证码key:{} ", key);
        return key;

    }


    @PostMapping("/send_code")
    public JsonData sendCode(@RequestBody SendCodeRequest sendCodeRequest, HttpServletRequest request) {

        String key = getCaptchaKey(request);
        String cacheCaptcha = redisTemplate.opsForValue().get(key);
        String captcha = sendCodeRequest.getCaptcha();
        if (null != cacheCaptcha && null != captcha && cacheCaptcha.equalsIgnoreCase(captcha)) {
            redisTemplate.delete(key);
            JsonData jsonData = notifyService.sendCode(SendCodeEnum.USER_REGISTER, sendCodeRequest.getTarget());
            return JsonData.buildSuccess(jsonData);
        } else {
            return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA_ERROR);
        }
    }


}
